package dwz.web.management.lotto;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import dwz.business.lotto.HuiYuanSheZhiInterface;
import dwz.business.lotto.YongHuInterface;
import dwz.business.lotto.ZhuDanInterface;
import dwz.common.util.FullSortUtils;
import dwz.framework.sys.exception.SystemClosedException;
import dwz.persistence.beans.WeiChongGongHaoMa;
import dwz.persistence.beans.WeiChongGongHaoMaExample;
import dwz.persistence.beans.YongHu;
import dwz.persistence.beans.ZhuDan;
import dwz.persistence.beans.ZhuDanExample;
import dwz.persistence.mapper.WeiChongGongHaoMaMapper;

import dwz.web.BaseController;
import dwz.web.vo.FronCountByQiHaoVO;
import dwz.web.vo.KuaiDaVO;
import dwz.web.vo.SearchConditionVO;
import dwz.web.vo.YonghuVo;
/**
 * 
 * 与注单相关的action
 *
 */
@Controller
@RequestMapping(value="/management/zhudan")
public class ZhuDanAction extends BaseController{
	@Autowired
	YongHuInterface yongHuInterface;
	@Autowired
	ZhuDanInterface zhuDanInterface;
	@Autowired
	HuiYuanSheZhiInterface huiYuanSheZhiInterface;
	@Autowired
	WeiChongGongHaoMaMapper weiChongGongHaoMaMapper;
	/**
	 * 关盘
	 * @return
	 */
	@RequestMapping("/guanpan")
	public String guanpan(){
		return "/management/front/guanpan";
	}
	
	/**
	 * 跳转到快打页面
	 * @return
	 */
	@RequestMapping("/kuaida")
	public String kuaida(){
		
		return "/management/front/kuaida";
	}
	
	/**
	 * 跳转到快打框
	 * @return
	 */
	@RequestMapping("/kuaidakuang")
	public String kuaidakuang(Model model,HttpServletRequest request){
		ZhuDanExample zhuDanExample = new ZhuDanExample();
		zhuDanExample.createCriteria().andYh_idEqualTo(getUserId(request));	
		zhuDanExample.setOrderByClause(" id DESC");
		List<ZhuDan> zhuDans = zhuDanInterface.selectTopTen(zhuDanExample);
		model.addAttribute("zhudans", zhuDans);	
		model.addAttribute("user",  yonghuInfo(request));
		return "/management/front/kuaidakuang";
	}
	
	
	/**
	 * 快打
	 * @param request
	 * @param kuaiDaVO
	 * @return
	 */
	@RequestMapping("/kuaidaxianzhu")
	public String kuaidaxianzhu(HttpServletRequest request,KuaiDaVO kuaiDaVO,Model model){
		try {
			String[] haomas = new String[1];
			kuaiDaVO.setNumber(kuaiDaVO.getNumber().replaceAll(" ", ""));
			String haoma = kuaiDaVO.getNumber();
			if(kuaiDaVO.getNumber().length() ==  4 && kuaiDaVO.getSizixian() != null && kuaiDaVO.getZhuan24() == null){
				haoma = kuaiDaVO.getNumber()+"X";
			}
			haomas[0] =  haoma;
			if(kuaiDaVO.getNumber().length() ==  4 && kuaiDaVO.getZhuan24() != null && kuaiDaVO.getZhuan24().equals("on")){
				//取出这四个数字
				String[] fourNumber = new String[4];
				
				for(int i = 0 ;i < 4 ;i++){
					fourNumber[i] = kuaiDaVO.getNumber().substring(i,i+1);
				}
				
				FullSortUtils fullSortUtils = new FullSortUtils(fourNumber);
				//24组4字定号码
				haomas =  fullSortUtils.getHaoMa();	
			}
			String ip = request.getRemoteAddr();
			Integer idInteger = (Integer)(request.getSession().getAttribute(CommonFields.USERID));
			yongHuInterface.xiaZhu(idInteger, haomas, kuaiDaVO.getMoney(), ip);
			ZhuDanExample zhuDanExample = new ZhuDanExample();
			//f4临时作为用户的id
			zhuDanExample.createCriteria().andYh_idEqualTo(idInteger);
			zhuDanExample.setOrderByClause(" id DESC ");
			List<ZhuDan> zhuDans = zhuDanInterface.selectTopTen(zhuDanExample);
			model.addAttribute("zhudans", zhuDans);
			model.addAttribute("user",  yonghuInfo(request));
		}catch (SystemClosedException e) {
			e.printStackTrace();
			return "/management/front/kuaidakuang";
		} 
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "/management/front/kuaidakuang";
		}
		return "/management/front/kuaidakuang";
		
	}
	/**
	 * 跳转到快选页面
	 * @return
	 */
	@RequestMapping("/kuaixuan")
	public String kuaixuan(){
		return "/management/front/kuaixuan";
	}
	/**
	 * 跳转到读取文件
	 * @return
	 */
	@RequestMapping("/duquwenjian")
	public String duquwenjian(){
		return "/management/front/duquwenjian";
	}
	/**
	 * 跳转到赔率变动页面 默认是四字定的。
	 * @return
	 */
	@RequestMapping("/peilvbiandongbiao")
	public String peilvbiandongbiao(){
		return "/management/front/peilvbiandongbiao";
	}
	

	/**
	 * 跳转到下注明细
	 * @return
	 */
	@RequestMapping("/xiazhumingxi")
	public String xiazhumingxi(HttpServletRequest request,Model model){
		model.addAttribute(CommonFields.QIHAO, getQiHao(request));
		return "/management/front/xiazhumingxi";
	}
	
	/**
	 * 从下注明细进入下注明细搜索
	 * @return
	 */
	@RequestMapping("xiazhumingxisousuo")
	public String xiazhumingxisousuo(Model model,SearchConditionVO searchConditionVO,HttpServletRequest request){
		return serachXiaZhuMingXin(model, searchConditionVO, request);
	}

	
	/**
	 * 从历史账单进入搜索页面
	 * @param model
	 * @param searchConditionVO
	 * @param request
	 * @return
	 */
	@RequestMapping("/fenzuxiazhumingxi/xiazhumingxisousuo")
	public String xiazhumingxisousuo2(Model model,SearchConditionVO searchConditionVO,HttpServletRequest request){
		return serachXiaZhuMingXin(model, searchConditionVO, request);
	}
	private String serachXiaZhuMingXin(Model model,
			SearchConditionVO searchConditionVO, HttpServletRequest request) {
		model.addAttribute("searchConditionVO", searchConditionVO);
		ZhuDanExample zhuDanExample = new ZhuDanExample();		
		buildConditions(zhuDanExample,searchConditionVO,request);		
		List<ZhuDan> zhuDans = zhuDanInterface.selectByExample(zhuDanExample);
		model.addAttribute("zhudans", zhuDans);
		
		return "/management/front/xiazhumingxi";
	}
	/**
	 * 构建搜索条件
	 * @param zhuDanExample
	 * @param searchConditionVO
	 */
	private void buildConditions(ZhuDanExample zhuDanExample,
			SearchConditionVO searchConditionVO,HttpServletRequest request) {
		dwz.persistence.beans.ZhuDanExample.Criteria criteria = zhuDanExample.createCriteria();
		criteria = criteria.andYh_idEqualTo(getUserId(request)).andQh_jEqualTo(Integer.parseInt(searchConditionVO.getQihao()));
		/**
		 * 号码 
		 */
		if(isNullOrBlank(searchConditionVO.getHaoma())){
			String haoMa = searchConditionVO.getHaoma();
			if(searchConditionVO.getHaoma().length() >=4  && searchConditionVO.getIsXian()!=null && searchConditionVO.getIsXian().equals("xian")){
				haoMa +="X";
			}
			
			criteria.andHm_jEqualTo(haoMa);
			
		}
		/**
		 *  处理金额范围
		 */
		if(isNullOrBlank(searchConditionVO.getMoney())){
			if(isNullOrBlank(searchConditionVO.getMoneyEnd())){
				criteria.andJe_jBetween(Float.parseFloat(searchConditionVO.getMoney()), Float.parseFloat(searchConditionVO.getMoneyEnd()));
			}else {
				criteria.andJe_jGreaterThan(Float.parseFloat(searchConditionVO.getMoney()));
			}
		}
		if(isNullOrBlank(searchConditionVO.getFenLei())){
			String lx = searchConditionVO.getFenLei();
			 if(lx.contains(",")){
				List<String> list = new ArrayList<String>();
				String[] iStrings  = lx.split(",");
				for(String string : iStrings){
					list.add(string);
				}
				//f4临时作为用户的id
				criteria.andLx_jIn(list);
			}else if(!lx.equals("ALL")){
				criteria.andLx_jEqualTo(searchConditionVO.getFenLei());
			}
		}
	}

	
	public boolean isNullOrBlank(String s){
		return (s!=null&&!s.replaceAll(" ", "").equals(""));
	}
	
	/**
	 * 跳转到赔率变动页面
	 * @return
	 */
	@RequestMapping("/lishizhangdan")
	public String lishizhangdan(Model model,String lx,HttpServletRequest request){
		ZhuDanExample zhuDanExample = new ZhuDanExample();
		
		if(lx!=null && !lx.equals("")){
			zhuDanExample.createCriteria().andYh_idEqualTo(getUserId(request)).andLx_jEqualTo(lx);
		}else {
			zhuDanExample.createCriteria().andYh_idEqualTo(getUserId(request));
		}
		List<ZhuDan> zhuDans = zhuDanInterface.selectByExample(zhuDanExample);
		model.addAttribute("zhudans", zhuDans);
		//__---以上信息无效
		YongHu yongHu = new YongHu();
		yongHu.setId(getUserId(request));
		List<FronCountByQiHaoVO> list = zhuDanInterface.frontCountByQiHao(yongHu);
		model.addAttribute("totals", list);
		return "/management/front/lishizhangdan";
	}
	/**
	 * 点击分组期号 跳转到详细页面
	 * @return
	 */
	@RequestMapping("/fenzuxiazhumingxi/{qihao}")
	public String xiazhumingxi2(@PathVariable Integer qihao,Model model){
		model.addAttribute(CommonFields.QIHAO, qihao);
		return "/management/front/xiazhumingxi";
	}
	
	
	
	
	/**
	 * 跳转到会员资料页面
	 * @return
	 */
	@RequestMapping("/huiyuanziliao")
	public String huiyuanziliao(Model model,HttpServletRequest request){
		YongHu yongHu = yongHuInterface.selectByPrimaryKey(getUserId(request));
		model.addAttribute("user", yongHu);
		return "/management/front/huiyuanziliao";
	}
	
	/**
	 * 会员资料修改
	 * @return
	 */
	@RequestMapping("/huiyuanziliaoxiugai")
	public String huiyuanziliaoxiugai(Model model,HttpServletRequest request){
		//fixstrhuishui_102 103  5 6 7 107
		//fixstrfrank_102 103  5 6 7 107
		//1-6  7-10  11 12 13 14
		//
		try {
			String hsPrefix = "fixstrhuishui_";
			String plPrefix = "fixstrfrank_";
			String[] hsPrefixStrings = new String[]{request.getParameter(hsPrefix+102),request.getParameter(hsPrefix+103),request.getParameter(hsPrefix+5),request.getParameter(hsPrefix+6),request.getParameter(hsPrefix+7),request.getParameter(hsPrefix+107)};
			String[] plPrefixStrings = new String[]{request.getParameter(plPrefix+102),request.getParameter(plPrefix+103),request.getParameter(plPrefix+5),request.getParameter(plPrefix+6),request.getParameter(plPrefix+7),request.getParameter(plPrefix+107)};
			huiYuanSheZhiInterface.updateUserProfile(hsPrefixStrings, plPrefixStrings, getUserId(request));
			model.addAttribute("erziding", request.getParameter(hsPrefix+102));
			model.addAttribute("sanziding", request.getParameter(hsPrefix+103));
			model.addAttribute("siziding", request.getParameter(hsPrefix+5));
			model.addAttribute("erzixian", request.getParameter(hsPrefix+6));
			model.addAttribute("sanzixian", request.getParameter(hsPrefix+7));
			model.addAttribute("sizixian", request.getParameter(hsPrefix+107));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/management/front/huiyuanziliao";
	}
	
	
	
	/**
	 * 跳转到规则说明页面
	 * @return
	 */
	@RequestMapping("/guizeshuoming")
	public String guizeshuoming(){
		return "/management/front/guizeshuoming";
	}
	
	/**
	 * 跳转到开奖号码页面
	 * @return
	 */
	@RequestMapping("/kaijianghaoma")
	public String kaijianghaoma(){
		return "/management/front/kaijianghaoma";
	}
	
	/**
	 * 跳转到 线上投注协议书页面
	 * @return
	 */
	@RequestMapping("/xianshangtouzhuxieyishu")
	public String xianshangtouzhuxieyishu(){
		return "/management/front/xianshangtouzhuxieyishu";
	}
	
	/**
	 * 跳转到 未成功号码页面
	 * @return
	 */
	@RequestMapping("/weichenggonghaoma")
	public String weichenggonghaoma(HttpServletRequest request,Model model){
		constructWeiChengGongHaoMa(request, model);
		return "/management/front/weichenggonghaoma";
	}
	
	/**
	 * 删除 未成功号码
	 * @return
	 */
	@RequestMapping("/shanchuweichenggonghaoma")
	public String shanchuweichenggonghaoma(HttpServletRequest request,Model model){
		String[] ids = request.getParameterValues("idarray");
		if(ids!=null && ids.length > 0){
			Integer[] idss = new Integer[ids.length];
			for(int i = 0 ; i<ids.length;i++){
				idss[i] = Integer.parseInt(ids[i]);
			}
			weiChongGongHaoMaMapper.batchDeleteById(idss);
		}		
		WeiChongGongHaoMaExample weiChongGongHaoMaExample = new WeiChongGongHaoMaExample();
		weiChongGongHaoMaExample.createCriteria()
		.andQh_jEqualTo((Integer)request.getSession().getAttribute(CommonFields.QIHAO))
		.andYh_id_jEqualTo((Integer)request.getSession().getAttribute(CommonFields.USERID));
		List<WeiChongGongHaoMa> haoMas = weiChongGongHaoMaMapper.selectByExample(weiChongGongHaoMaExample);
		model.addAttribute("weis", haoMas);
		return "/management/front/weichenggonghaoma";
	}

	private void constructWeiChengGongHaoMa(HttpServletRequest request,
			Model model) {
		WeiChongGongHaoMaExample weiChongGongHaoMaExample = new WeiChongGongHaoMaExample();
		weiChongGongHaoMaExample.createCriteria()
		.andQh_jEqualTo((Integer)request.getSession().getAttribute(CommonFields.QIHAO))
		.andYh_id_jEqualTo((Integer)request.getSession().getAttribute(CommonFields.USERID));
		List<WeiChongGongHaoMa> haoMas = weiChongGongHaoMaMapper.selectByExample(weiChongGongHaoMaExample);
		model.addAttribute("weis", haoMas);
	}
	
	
	/**
	 * 跳转到  快选号码选择页面
	 * @return
	 */
	@RequestMapping("/kuaixuansection")
	public String kuaixuansection(){
		return "/management/front/kuaixuansection";
	}
	/**
	 * 快选下注
	 * @param request
	 * @return
	 */
	@RequestMapping("/kuanxuanxiazhu")
	public String kuaiXuanXiaZhu(HttpServletRequest request){
		try {
			String[] haomas = request.getParameterValues("selectnumber");
			String jinEr = request.getParameter("post_money");
			if(haomas == null  || jinEr == null){
				return "/management/front/kuaixuansection";
			}
			haomas = haomas[0].split(",");
			/**
			 * 将四字现处理成 1234X形式。
			 */
			String isSiZiXian = request.getParameter("sizixian");
			if(isSiZiXian != null && isSiZiXian.equals("on")){
				for(int i = 0; i < haomas.length; i++){
					haomas[i] = haomas[i]+"X";
				}
			}
			
			Float xiaZhuJinEr = Float.parseFloat(jinEr);
			yongHuInterface.xiaZhu(getUserId(request), haomas, xiaZhuJinEr, request.getRemoteAddr());
		} catch (Exception e) {
			e.printStackTrace();
			return "/management/front/shengchengkuang";
		}
		return "/management/front/shengchengkuang";
	}
	
	/**
	 * 快打下注
	 * @return
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	@RequestMapping("/xiazhu")
	public String xiazhu(HttpServletRequest request,KuaiDaVO kuaiDaVO) throws NumberFormatException, Exception{
		
		String[] haomas = new String[]{"1234"};
		System.out.println(kuaiDaVO.toString());
		yongHuInterface.xiaZhu(getUserId(request), haomas, 12.5f, request.getRemoteAddr());
		//TODO 跳转的路径不一样。
		return "/management/front/kuaixuansection";
	}
	/**
	 * ajax 返回号码的赔率以及可下的金额
	 * @param request
	 * @return
	 */
	@RequestMapping("/haomaxianzhi")
	public @ResponseBody String findpeilv(HttpServletRequest request){
		String haoMaXianzhi = "1|100|1000|189";
		return haoMaXianzhi;
	}
	/**
	 * 退码
	 * @return
	 */
	@RequestMapping("/tuima")
	public String tuima(HttpServletRequest request){
		return "/management/front/kuaixuansection";
	}
	@RequestMapping("/tuimaAjax")
	public @ResponseBody String tuimaAjax(HttpServletRequest request){
		String success = "ok";
		try {
			String[] ids = request.getParameterValues("haomas[]");
			Integer[] integers = new Integer[ids.length];
			int i = 0;
			for(String id : ids){
				integers[i++] = Integer.parseInt(id);
			}
			yongHuInterface.tuiMa(getUserId(request), integers);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
			return e.toString();
		}
		return success;
	}
	
	@RequestMapping("/shanchuweichenggong")
	public String shanchuweichenggong(HttpServletRequest request){
		return "/management/front/kuaixuansection";
	}
	/**
	 * 跳转到生成框页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/shengchengkuang")
	public String shengchengkuang(HttpServletRequest request){
		return "/management/front/shengchengkuang";
	}
	
	/**
	 * 跳转到生成框页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/ooxx")
	public String ooxx(HttpServletRequest request){
		//生成所有二字号码以及赔率 如果 这些号码在赔率变动表中则按照赔率变动表中的赔率来算。
		
		
		
		return "/management/front/ooxx";
	}
	/**
	 * 跳转到生成框页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/ooxx2")
	public String ooxx2(HttpServletRequest request){
		
		return "/management/front/ooxx2";
	}
	
	/**
	 * 跳转到顶部的通知
	 * @param request
	 * @return
	 */
	@RequestMapping("/news")
	public String news(HttpServletRequest request){
		return "/management/front/news";
	}	
	
	
	private Integer getUserId(HttpServletRequest request){
		return (Integer)request.getSession().getAttribute(CommonFields.USERID);
	}
	
	private Integer getQiHao(HttpServletRequest request){
		return (Integer)request.getSession().getAttribute(CommonFields.QIHAO);
	}
	
	private Integer getQiHaoFromParameter(HttpServletRequest request){
		return Integer.parseInt(request.getParameter(CommonFields.QIHAO));
	}
	/**
	 * 用户信息
	 * @param request
	 * @return
	 */
	private YongHu yonghuInfo(HttpServletRequest request){
		YongHu yongHu = yongHuInterface.selectByPrimaryKey((Integer) request.getSession().getAttribute(CommonFields.USERID));
		return yongHu;
		
	}
}
